import { effectFn } from "../reactivity/effect";
import { queuePreFlushCb } from "./scheduler";

export function watchEffect(source) {

    function job() {
        effect.run()
    }

    let cleanup
    const onCleanup = function(fn) {
        cleanup = effect.onStop = () => {
            fn()
        }
    }

    function getter() {
        if(cleanup) {
            cleanup()
        }
        source(onCleanup)
    }

    const effect = new effectFn(getter,() => {
        queuePreFlushCb(job)
    })

    effect.run()

    return () => {
        effect.stop()
    }

}